package jmine.tec.rpc.server.representation.representers.reflection;

import java.beans.PropertyDescriptor;

import jmine.tec.rpc.server.representation.RepresentationContext;

import org.apache.commons.beanutils.PropertyUtils;

/**
 * Representador de propriedades quaisquer
 * 
 * @author lundberg
 * @param <R> tipo de representação
 * @param <E> tipo de entidade representada
 */
public class AnyPropertyRepresenter<R, E> implements PropertyRepresenter<R, E> {

    /**
     * {@inheritDoc}
     */
    public boolean isApplicableTo(PropertyDescriptor representationProperty) {
        return RepresenterUtil.isBeanProperty(representationProperty);
    }

    /**
     * {@inheritDoc}
     */
    public void representProperty(RepresentationContext context, R representation, PropertyDescriptor representationProperty, E object)
            throws Exception {
        Object value = PropertyUtils.getProperty(object, representationProperty.getName());
        Object valueRepresentation = context.representObject(RepresenterUtil.getPropertyType(representationProperty), value);
        PropertyUtils.setProperty(representation, representationProperty.getName(), valueRepresentation);
    }
}
